package com.test.demo;

/**
 * Created by jl on 2017/11/9.
 */
public class MyBubble {

    public static void bubbleSort(int[] arr) {
        int len = arr.length;
        int innerLimit = len - 1;
        for(int i = 0; i < len; i++) {
            for(int j = 0; j < innerLimit; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                }
            }
            innerLimit--;
        }
    }

    private static void swap(int[] arr, int x, int y) {
        int tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }

    public static void main(String[] args) {
        int[] arr = {2, 5, 3, 1, 4};
        bubbleSort(arr);
        for (int a : arr) {
            System.out.println(a);
        }
    }
}
